//
//  BluetoothConnectHelper.m
//  firstCordova
//
//  Created by catch on 16/9/19.
//
//

#import "BluetoothConnectHelper.h"
#import "MBBlConnectViewController.h"
#import "BLECentralManager.h"
#import "MBNotification+MZero.h"

@interface BluetoothConnectHelper ()<BLEControllerDelegate,MBBlConnectViewControllerDelegate>

@property (nonatomic, strong) CDVInvokedUrlCommand* connectedCmd;
@property (nonatomic, strong) CDVInvokedUrlCommand* disconnectedCmd;
@property (nonatomic, strong) CDVInvokedUrlCommand* receivedDataCmd;
@property (nonatomic, strong) CDVInvokedUrlCommand* backButtonClickedCmd;

@end

@implementation BluetoothConnectHelper

-(void)pluginInitialize{
    [super pluginInitialize];
    
    //监听蓝牙回调
    [[BLECentralManager sharedManager] addDelegate:self];
    
    [MBNotification addObserver:self selector:@selector(handleBleDisconnected) name:MBN_MCUDisconnected];
}

-(void)dealloc{
    [MBNotification removeObserver:self];
}

#pragma mark -- Public Methods

-(void)showBluetoothConnectScence:(CDVInvokedUrlCommand *)command{
    //先断开连接
//    [[BLECentralManager sharedManager] disconnectActivePeripheral];
    
    MBBlConnectViewController* vc = [MBBlConnectViewController sharedInstance];
    vc.delegate = self;
    
    if (vc && self.viewController) {
        if (![vc canPush]) {
            return;
        }
        
        [self.viewController presentViewController:vc animated:YES completion:^{
            CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"成功跳转到蓝牙连接界面"];
            [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
        }];
        
    }else{
        CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"跳转到蓝牙连接界面失败!"];
        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    }
}

-(void)dismissBluetoothConnectScence:(CDVInvokedUrlCommand *)command{
    MBBlConnectViewController* vc = [MBBlConnectViewController sharedInstance];
    if (![vc canPush]) {//在前端
        [vc dismissViewControllerAnimated:YES completion:^{
            CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"退出蓝牙连接界面"];
            [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
        }];
    }else{
        CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"您未在蓝牙连接界面无法退出!"];
        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    }
}

-(void)sendData:(CDVInvokedUrlCommand *)command{
    NSData *message = [command.arguments firstObject];
    if (message && [BLECentralManager sharedManager].activePeripheral) {
        [[BLECentralManager sharedManager] sendData:message];
        
        CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"成功发送数据"];
        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
        
    }else{
        CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"发送数据失败，请先检查发送的数据是否为空以及蓝牙是否已连接！！"];
        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    }
}

-(void)disConnect:(CDVInvokedUrlCommand *)command{
    [[BLECentralManager sharedManager] disconnectActivePeripheral];
}

-(void)startListenConnected:(CDVInvokedUrlCommand *)command{
    self.connectedCmd = command;
}

-(void)startListenDisconnected:(CDVInvokedUrlCommand *)command{
    self.disconnectedCmd = command;
}

-(void)startListenReceivedData:(CDVInvokedUrlCommand *)command{
    self.receivedDataCmd = command;
}

-(void)startListenBackButtonClicked:(CDVInvokedUrlCommand *)command{
    self.backButtonClickedCmd = command;
}

-(void)handleBleDisconnected{
    if(_disconnectedCmd){
        CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"已断开连接！"];
        [pluginResult setKeepCallbackAsBool:YES];
        [self.commandDelegate sendPluginResult:pluginResult callbackId:_disconnectedCmd.callbackId];
    }
}

#pragma mark -- BLEControllerDelegate

-(void)bleConnected{
    if(_connectedCmd){
        CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"已连接了蓝牙！"];
        [pluginResult setKeepCallbackAsBool:YES];
        [self.commandDelegate sendPluginResult:pluginResult callbackId:_connectedCmd.callbackId];
    }
}

-(void)bleReceivedData:(NSData*)data{
    if(_receivedDataCmd){
        CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArrayBuffer:data];
        [pluginResult setKeepCallbackAsBool:YES];
        [self.commandDelegate sendPluginResult:pluginResult callbackId:_receivedDataCmd.callbackId];
    }
}

#pragma mark -- MBBlConnectViewControllerDelegate

- (void)backButtonClicked{
    CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"点击退出按钮，退出蓝牙连接界面"];
    [pluginResult setKeepCallbackAsBool:YES];
    [self.commandDelegate sendPluginResult:pluginResult callbackId:_backButtonClickedCmd.callbackId];
}

@end
